package com.ruoyi.biz.service.impl;

import java.util.List;

import com.ruoyi.biz.domain.ReportType;
import com.ruoyi.biz.domain.reportAndType;
import com.ruoyi.biz.mapper.reportAndTypeMapper;
import com.ruoyi.biz.service.IReportTypeService;
import com.ruoyi.biz.service.IreportAndTypeService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ihome.domain.IhHome;
import com.ruoyi.ihome.domain.IhHomeTag;
import com.ruoyi.ihome.domain.TagAndHome;
import com.ruoyi.ihome.service.IIhHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.mapper.IhReportMapper;
import com.ruoyi.biz.domain.IhReport;
import com.ruoyi.biz.service.IIhReportService;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * 举报信息表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-15
 */
@Service

public class IhReportServiceImpl implements IIhReportService {
    @Resource
    private IhReportMapper ihReportMapper;
    @Resource
    private reportAndTypeMapper reportAndTypeMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private IIhHomeService homeService;
//    @Autowired
//    private IReportTypeService reportTypeService;

    /**
     * 查询举报信息表
     *
     * @param id 举报信息表主键
     * @return 举报信息表
     */
    @Override
    public IhReport selectIhReportById(Long id) {
        return ihReportMapper.selectIhReportById(id);
    }

    /**
     * 查询举报信息表列表
     *
     * @param ihReport 举报信息表
     * @return 举报信息表
     */
    @Override
    public List<IhReport> selectIhReportList(IhReport ihReport) {
        return ihReportMapper.selectIhReportList(ihReport);
    }

    /**
     * 新增举报信息表
     *
     * @param ihReport 举报信息表
     * @return 结果
     */
    @Override
    public int insertIhReport(@RequestBody IhReport ihReport) {
//        System.out.println(ihReport.toString());

//        ihHome.setCreateTime(DateUtils.getNowDate());
//        int num = ihHomeMapper.insertIhHome(ihHome);
//        Long houseId = ihHome.getId();
//        Long[] ihHomeTag = ihHome.getIhHomeTag();
//        for (Long tagId : ihHomeTag){
//            num = num + tagAndHomeMapper.insertTagAndHome(new TagAndHome(null,houseId,tagId));
//        }
//        Long[] ihHomeFacility = ihHome.getIhHomeFacility();
//        return num;
        int i = ihReportMapper.insertIhReport(ihReport);
        Long reportInfoId = ihReport.getId();
        Long[] reportTypeIds = ihReport.getReportTypeIds();
        //被举报主体
        Long whoReport = ihReport.getWhoReport();
        //被举报的房子id或用户id
        Long userHouseId = ihReport.getUserHouseId();
        Long userId = whoReport==1?userHouseId:(homeService.selectIhHomeById(userHouseId)).getUserId();
        for (Long reportTypeId : reportTypeIds) {
            reportAndTypeMapper.insertreportAndType(new reportAndType(reportTypeId, reportInfoId));
            redisTemplate.opsForZSet().incrementScore("sys_int:integral",userId,-5);
        }
        return i;
    }

    /**
     * 修改举报信息表
     *
     * @param ihReport 举报信息表
     * @return 结果
     */
    @Override
    public int updateIhReport(IhReport ihReport) {
        return ihReportMapper.updateIhReport(ihReport);
    }

    /**
     * 批量删除举报信息表
     *
     * @param ids 需要删除的举报信息表主键
     * @return 结果
     */
    @Override
    public int deleteIhReportByIds(Long[] ids) {
        return ihReportMapper.deleteIhReportByIds(ids);
    }

    /**
     * 删除举报信息表信息
     *
     * @param id 举报信息表主键
     * @return 结果
     */
    @Override
    public int deleteIhReportById(Long id) {
        return ihReportMapper.deleteIhReportById(id);
    }
}
